<?php 

/**
* 
*/
class MessageBO 
{

    /**
     * 组织新活动提醒
     * @param $openids
     * @param $actid
     * @return array
     */
	static function newActivityOfOrg($openids,$actid){

        // 消息模版id
        $templateid="4prieEMgug2OWvcylfwcLyZpIJQtC5vAO4wtAUI11Mw";

		$data = array('code' =>1 , 'message' => '发送失败');

		try {

			// 检查 openid 是否为空
			if(empty($openids)) throw new Exception("openid is null", 1);		

			// 查看活动编号是否为0
			if(empty($actid) || $actid==0) throw new Exception("activity id is null", 1);

			print_log("--openids:".$openids);
			print_log("--actid:".$actid);


            // 获取活动信息
			$acinfo=ActivityModel::info($actid);

			if(empty($acinfo)) throw new Exception("activity is null for id:$actid");

			// 组合消息内容
			$msg_data=array();

			$msg_data["first"] = array('value' => "您的组织发布了新活动",'color' => '#0000FF' );
			$msg_data["keyword1"] = array('value' => $acinfo["name"],'color' => '#0000FF' );
			$msg_data["keyword2"] = array('value' => $acinfo["startDay"]." ".$acinfo["startHour"].":".$acinfo["startMinute"],'color' => '#0000FF' );
			$msg_data["keyword3"] = array('value' => $acinfo["city"].$acinfo["district"].$acinfo["address"],'color' => '#0000FF' );
			$msg_data["keyword4"] = array('value' => $acinfo["needs"]."人",'color' => '#0000FF' );
			// $msg_data["remark"] = array('value' => "查看详细信息请点击详情",'color' => '#0000FF' );

			
			$message["template_id"]=$templateid;
			$message["url"]=getConfig("url_activity_info")."?activityId=$actid";
			$message["topcolor"]="#FF0000";
			$message["data"]=$msg_data;


			$sendMessage="";

			$id_array=json_decode($openids,true);

			// print_log("--id_array:".$id_array);

			if(!is_array($id_array))$id_array=json_decode($id_array,true);

			// print_log(is_array($id_array));
			
			// 循环发送消息
			foreach ($id_array as $key => $value) {

				$openid=$value;
				
				$message["touser"]=$openid;

				// 发送消息
				$result=WeixinUtil::sendTemplateMessage($message);

				// 记录发送信息 
				$sendMessage=$sendMessage."\n openid:$openid, 发送成功.";

				if($result["errcode"]!=0){

					// $data = array('code' =>$result["errcode"] , 'message' => '发送失败.'.$result["errmsg"]);

					$sendMessage=$sendMessage."\n openid:$openid, 发送失败!.".$result["errmsg"];
				}

			}

			$data["message"]=$sendMessage;

            print_log(" result message:".$sendMessage);

			
		} catch (Exception $e) {

			$data = array('code' =>0 , 'message' => '发送失败.'.$e->getMessage());
			
		}

		return $data;

	}


	// 明日活动提醒
	static function tomorrowActiviys($acinfoArray){

        print_log("acinfoArray:".$acinfoArray);

		// 消息模版id
		$templateid="JOP-ewdgH3Eg2CZHVt2w7Wtvilb5jyO08nutQT5kBMk";

		$data = array('code' =>1 , 'message' => '发送失败');

		$sendMessage="";

		try {

			// 活动信息数组不能为空 
			if(empty($acinfoArray)) throw new Exception("活动信息参数为空", 1);

			// 解析数组
			$acinfo_array=json_decode($acinfoArray,true);

			if(!is_array($acinfo_array)) $acinfo_array=json_decode($acinfo_array);


			//循环数组
			foreach ($acinfo_array as $key => $value) {
				
				$acinfo=$value;

				$acid=$acinfo["id"];
				$openid_array=$acinfo["openids"];

				// 获取活动信息 
				$acinfo=ActivityModel::info($acid);

				if(empty($acinfo)){

					$sendMessage=$sendMessage."\n acid:$acid, 发送失败! 活动不存在";

					continue;

				} 				

				// 循环用户
				foreach ($openid_array as $id => $openid) {

					// 通过openid 获取 会员id
					$m_model=new MemberModel();

					$memberid=$m_model->getMemberIdByOpenId($openid);

					$minfo=$m_model->info($memberid);

					if(empty($minfo)){

						$sendMessage=$sendMessage." openid:$openid, 发送失败! 会员信息不存在";

						continue;
					}
					
					// 组合消息内容
					$msg_data=array();

                    //获得明天日期
                    $tomorrow_date=date("Y-m-d",strtotime("+1 day"));

					$msg_data["first"] = array('value' => "活动提醒",'color' => '#0000FF' );
					$msg_data["keyword1"] = array('value' => $minfo["realName"],'color' => '#0000FF' );
					$msg_data["keyword2"] = array('value' => $acinfo["name"],'color' => '#0000FF' );
					$msg_data["keyword3"] = array('value' => $tomorrow_date." ".$acinfo["startHour"].":".$acinfo["startMinute"],'color' => '#0000FF' );
					$msg_data["keyword4"] = array('value' => $acinfo["city"].$acinfo["district"].$acinfo["address"],'color' => '#0000FF' );
					$msg_data["remark"] = array('value' => "活动即将开始，请准时参加哦",'color' => '#0000FF' );


					$message["touser"]=$openid;
					$message["template_id"]=$templateid;
					$message["topcolor"]="#FF0000";
					$message["data"]=$msg_data;

					// 发送消息
					$result=WeixinUtil::sendTemplateMessage($message);

					// 记录发送信息 
					$sendMessage=$sendMessage."\n openid:$openid, 发送成功.";

					if($result["errcode"]!=0){

						$sendMessage=$sendMessage."\n openid:$openid, 发送失败!.".$result["errmsg"];
					}

				}

			}

            print_log(" result message:".$sendMessage);

			$data["message"]=$sendMessage;	
			
		} catch (Exception $e) {

			$data = array('code' =>0 , 'message' => '发送失败.'.$e->getMessage());
			
		}

		return $data;


	}



    /**
     * 组织报名审核结果
     * @param $orgid
     * @param $openid
     * @param $check_result
     * @return array
     */
    static function joinOrgCheckResult($orgid,$openid,$check_result){

        // 消息模版id
        $templateid="u1VAbxzQJA5ZPVYR1G75iSOy95Vn1T4PYG7ReqyYuf0";

        $data = array('code' =>1 , 'message' => '发送失败');

        try{

            $msg_data=MessageBO::getJoinOrgCheckResultMsgData($orgid,$openid,$check_result);

            $message=array();

            //组合消息内容
            $message["touser"]=$openid;
            $message["template_id"]=$templateid;
            $message["topcolor"]="#FF0000";
            $message["data"]=$msg_data;

            //发送消息
            $result=WeixinUtil::sendTemplateMessage($message);

            if($result["errcode"]!=0){

                throw new Exception($result["errmsg"]);
            }

        }catch (Exception $e){

            $data = array('code' =>0 , 'message' => '发送失败.'.$e->getMessage());
        }


		return $data;
	}


    /**
     * 活动报名审核结果
     * @param $acid
     * @param $openid
     * @param $check_result
     * @return array
     */
	static function joinActivityCheckResult($acid,$openid,$check_result){

        // 消息模版id
        $templateid="5SZg9nbbWYTGP4x63u3uz6-pP8nkS5n9JA9tnbfpxbg";

        $data = array('code' =>1 , 'message' => '发送失败');

        try{

            $msg_data=MessageBO::joinActivityCheckResultMsgData($acid,$check_result);

            $message=array();

            //组合消息内容
            $message["touser"]=$openid;
            $message["template_id"]=$templateid;
            $message["topcolor"]="#FF0000";
            $message["data"]=$msg_data;

            //发送消息
            $result=WeixinUtil::sendTemplateMessage($message);

            if($result["errcode"]!=0){

                throw new Exception($result["errmsg"]);
            }

        }catch (Exception $e){

            $data = array('code' =>0 , 'message' => '发送失败.'.$e->getMessage());
        }

		return $data;
	}


    /**
     * 生成格式化内容，活动报名审核结果通知
     * @param $acid
     * @param $check_result
     * @return array
     */
    static private function joinActivityCheckResultMsgData($acid,$check_result){

        $msg_data=array();

        $title="您好，您的报名已经通过主办方审核";

        $str_status="通过审核";

        if(!$check_result){

            $title="抱歉，您的活动报名申请被拒绝了";

            $str_status="被拒绝";
        }

        //获得活动信息
        $acinfo=ActivityModel::info($acid);

        //通知标题
        $msg_data["first"] = array('value' => $title,'color' => '#0000FF' );

        //活动名称
        $msg_data["keyword1"] = array('value' => $acinfo["name"],'color' => '#0000FF' );

        //活动时间
        $msg_data["keyword2"] = array('value' => $acinfo["startDay"]." ".$acinfo["startHour"].":".$acinfo["startMinute"],'color' => '#0000FF' );

        //活动地点
        $msg_data["keyword3"] = array('value' => $acinfo["city"].$acinfo["district"].$acinfo["address"],'color' => '#0000FF' );

        //审核状态
        $msg_data["keyword4"] = array('value' => $str_status,'color' => '#0000FF' );

        //备注信息
        $msg_data["remark"] = array('value' => "",'color' => '#0000FF' );


        return $msg_data;
    }



    /**
     * 生成格式化内容。加入组织审核结果通知
     * @param $orgid
     * @param $openid
     * @param $check_result
     * @return array
     */
    static private function getJoinOrgCheckResultMsgData($orgid, $openid, $check_result){

        //获得组织信息
        $orgInfo=OrgModel::info($orgid);

        //获得会员信息
        $memberModel=new MemberModel();

        $mid=$memberModel->getMemberIdByOpenId($openid);

        $memberInfo=$memberModel->info($mid);

        //组合信息内容
        $title="您好，您加入".$orgInfo["shortname"]."的申请已通过";

        if(!$check_result){

            $title="抱歉，您加入".$orgInfo["shortname"]."的申请被拒绝了";
        }

        $msg_data=array();

        //通知标题
        $msg_data["first"] = array('value' => $title,'color' => '#0000FF' );

        //会员姓名
        $msg_data["keyword1"] = array('value' => $memberInfo["realName"],'color' => '#0000FF' );

        //会员手机号码
        $msg_data["keyword2"] = array('value' => $memberInfo["mobile"],'color' => '#0000FF' );

        //审核时间
        $msg_data["keyword3"] = array('value' => date("Y-m-d h:s"),'color' => '#0000FF' );

        //备注信息
        $msg_data["remark"] = array('value' => "",'color' => '#0000FF' );

        return $msg_data;

    }


}

